#!/usr/bin/python
#***************************************************************************
# Copyright notice for this file:
#  Copyright (C) 2012 TheAxeMan
#  _______ _                               _    _
# |__   __| |     ___     /\          ___ | \  / |
#    | |  | |__  / _ \   /  \  __  _ / _ \|  \/  | __ _ _ __
#    | |  | '_ \|  __/  / /\ \ \ \/ |  __/| .  . |/ _` | '_ \
#    | |  | | | | (__/\/ ____ \ )  (| (__/| |\/| | (_| | | | |
#    |_|  |_| |_|\____/_/    \_._/\_.\____|_|  |_|\__,_|_| |_|
#
# Final Fantasy TAS scripts by TheAxeMan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#***************************************************************************

from inputgeneration import getRunThroughNames, CharCoordMap, CharType


def checkNames(names):
   try:
      if len(names) < 4:
         return False, "Four names are required"
      for name in names:
         if len(name) > 4:
            return False, "%s is too long, only four characters allowed" % (name,)
         for c in name:
            if c not in CharCoordMap:
               return False, "Invalid character used: %s" % (c,)
      
   except:
      return False, "Invalid names"
   
   return True, ''

def generateRun(names, whitemage=False, finalorder=False):
   charClass = CharType.whitemage if whitemage else CharType.fighter
   if finalorder:
      if whitemage:
         names = [names[2], names[3], names[1], names[0]]
      else:
         names = [names[3], names[1], names[0], names[2]]
   
   runText = getRunThroughNames(names, charClass=charClass)
   
   from zipfile import ZipFile
   zf = ZipFile("rundata.zip")
   
   if not whitemage:
      subtitleOffset = len(runText.split()) - 208
      subtitleFile = zf.open("ffsubtitles.txt")
      subtitleText = ""
      for line in subtitleFile.readlines():
         if line.strip() == '':
            continue
         command, frame = line[:15].split()
         newframe = int(frame) + subtitleOffset
         if newframe < 1:
            continue
         subtitleText += "%s %s %s\n" % (command, newframe, line[16:].strip())
      subtitleFile.close()
      runText = getRunThroughNames(names, charClass=charClass, subtitles=subtitleText)
      
   runFile = "FinalFantasy-wm-afternames.fm2" if whitemage else "FinalFantasy-LightWarriors-afternames.fm2"
   runInputFile = zf.open(runFile)
   for line in runInputFile.readlines():
      runText += line
   runInputFile.close()
   
   zf.close()

   return runText
   

def main():
   from optparse import OptionParser

   parser = OptionParser()
   parser.add_option("-w", "--whitemage", action="store_true",
                     help="Generate white mage run")
   parser.add_option("-i", "--initialorder", action="store_true",
                     help="Names given in initial order rather than final order")

   (options, args) = parser.parse_args()
   
   valid, msg = checkNames(args)
   if not valid:
      print msg
      return
   
   print generateRun(args, options.whitemage, not options.initialorder)
   

if __name__ == '__main__':
   main()
